package com.mrliu.undertow.controller;


import com.mrliu.undertow.pojo.UserInfo;
import com.mrliu.undertow.service.UserInfoService;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author liuyangos8888
 */
@Api(tags = "用户操作功能接口")
@RestController
@RequestMapping("/user")
public class UserInfoController {

    private static Logger log = LoggerFactory.getLogger(UserInfoController.class);

    @Resource
    private UserInfoService userInfoService;

    /**
     * 查询所有数据
     *
     * @return 所有数据
     */
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "接口返回成功状态"),
            @ApiResponse(code = 500, message = "接口返回未知错误，请联系开发人员调试")
    })
    @ApiOperation(value = "用户全查接口", notes = "访问此接口，返回hello语句，测试接口")
    @GetMapping("/selectList")
    public List<UserInfo> selectList() {
        return userInfoService.selectList();
    }


    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "接口返回成功状态"),
            @ApiResponse(code = 500, message = "接口返回未知错误，请联系开发人员调试")
    })
    @ApiOperation(value = "JSP全查接口", notes = "访问此接口，返回hello语句，测试接口")
    @RequestMapping(value = "/selectList2", produces = "application/json;charset=UTF-8", method = {RequestMethod.GET})
    public void selectList2(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<UserInfo> list = userInfoService.selectList();

        List<Map<String, Object>> mapList = getMapsResult(list);

        log.info("进入了selectList2方法！");

        ModelAndView mav = new ModelAndView("jspIndex.jsp");
        mav.addObject("list", mapList);
        request.setAttribute("list", mapList);
        request.getRequestDispatcher("/WEB-INF/jsp/jspIndex.jsp").forward(request, response);
    }



    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "接口返回成功状态"),
            @ApiResponse(code = 500, message = "接口返回未知错误，请联系开发人员调试")
    })
    @ApiOperation(value = "自定义JSP全查接口", notes = "访问此接口，返回hello语句，测试接口")
    @RequestMapping(value = "/selectList3", produces = "application/json;charset=UTF-8", method = {RequestMethod.GET})
    public void selectList3(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<UserInfo> list = new ArrayList<>();

        UserInfo userInfo = new UserInfo();
        userInfo.setId(2);
        userInfo.setUserName("李四");
        userInfo.setUserAge("112");
        userInfo.setUserSex("男");
        userInfo.setUserIdCard("10001199104131278");
        userInfo.setUserPhone("1371452312");
        userInfo.setUserFrom("浙江");
        userInfo.setUserMinZu("汉族");
        userInfo.setUserAddress("某某大陆某某大街2220号");
        userInfo.setUserZhiYe("教师");
        userInfo.setUserEducate("博士");

        userInfo.setCreateDate("2020-05-17 22:22:22");
        userInfo.setUpdateDate("2020-05-17 22:22:22");
        userInfo.setIdDeleted(0);
        list.add(userInfo);

        log.info("进入了selectList3方法！");

        ModelAndView mav = new ModelAndView("jspIndex.jsp");
        mav.addObject("list", list);
        request.setAttribute("list", list);
        request.getRequestDispatcher("/WEB-INF/jsp/jspIndex.jsp").forward(request, response);
    }


    private List<Map<String, Object>> getMapsResult(List<UserInfo> list) {
        List<Map<String, Object>> mapList = new ArrayList<>();

        for (UserInfo userInfo : list) {

            Map<String, Object> map = new LinkedHashMap<>();

            map.put("id", userInfo.getId());
            map.put("userName", userInfo.getUserName());
            map.put("userAge", userInfo.getUserAge());
            map.put("userSex", userInfo.getUserSex());
            map.put("userIdCard", userInfo.getUserIdCard());
            map.put("userPhone", userInfo.getUserPhone());
            map.put("userFrom", userInfo.getUserFrom());
            map.put("userMinZu", userInfo.getUserMinZu());
            map.put("userAddress", userInfo.getUserAddress());
            map.put("userEducate", userInfo.getUserEducate());
            map.put("userZhiYe", userInfo.getUserZhiYe());
            map.put("updateDate", userInfo.getUpdateDate());
            map.put("createDate", userInfo.getCreateDate());
            map.put("idDeleted", userInfo.getIdDeleted());
            mapList.add(map);
        }
        return mapList;
    }

}